package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.AchievementFileMapper;
import com.hbnu.system.model.entity.Achievement;
import com.hbnu.system.model.entity.AchievementFile;
import com.hbnu.system.service.AchievementFileService;
import com.hbnu.system.service.AchievementService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.BindException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * (AchievementFile)表服务实现类
 *
 * @author makejava
 * @since 2025-07-22 22:48:48
 */
@Service("achievementFileService")
public class AchievementFileServiceImpl extends ServiceImpl<AchievementFileMapper, AchievementFile> implements AchievementFileService {

    @Autowired
    FileUploadUtil fileUploadUtil;
    @Autowired
    AchievementService iAchievementService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;

    /**
     * 新增附件
     * @param achievementFile
     * @return
     */
    @Override
    public boolean addAchievementFile(AchievementFile achievementFile) {
        System.out.println("addAchievementFile"+achievementFile);
        Achievement byId = iAchievementService.getById(achievementFile.getAchievementid());
        System.out.println("byId"+byId);
        checkRoleUtil.checkPermissionAchievement(byId.getStatus());
        achievementFile.setFiledate(LocalDateTime.now());
        if (byId.getStatus().contains("审核")){
            throw new BaseException("该状态不可提交附件!");
        }
        achievementFile.setFiledate(LocalDateTime.now());
        return baseMapper.insert(achievementFile)==1;
    }

    @Override
    public AchievementFile getAchievementFile(String id) {
        AchievementFile achievementFile = super.getOne(Wrappers.lambdaQuery(AchievementFile.class)
                .eq(AchievementFile::getFileid,id));
        if(Objects.isNull(achievementFile)){
            throw new BaseException("文件编号异常!未查找到该文件信息");
        }
        return achievementFile;
    }

    @Override
    public List<AchievementFile> pageAchievementFile(String id) {
        if(StrUtil.isBlank(id)){
            throw new BaseException("论文编号为空值");
        }
        List<AchievementFile> achievementFileList = baseMapper.selectList(Wrappers.lambdaQuery(AchievementFile.class)
                .eq(AchievementFile::getAchievementid,id));
        return achievementFileList;
    }

    /**
     * 删除附件
     * @param id
     * @return
     * @throws IOException
     */
    @Override
    public boolean deletAchievementFile(String id) throws IOException {
        AchievementFile one = super.getOne(Wrappers.lambdaQuery(AchievementFile.class)
                .eq(AchievementFile::getFileid, id));
        if (Objects.isNull(one)){
            throw new BaseException("文件编号有误!");
        }
        Achievement achievement = iAchievementService.getById(one.getAchievementid());
        checkRoleUtil.checkPermissionAchievement(achievement.getStatus());
        boolean delete = fileUploadUtil.delete(one.getFileurl());
        if(!delete){
            throw new BindException("附件删除失败");
        }
        return baseMapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateAchievementFile(AchievementFile achievementFile) {
        Achievement achievement = iAchievementService.getById(achievementFile.getAchievementid());
        checkRoleUtil.checkPermissionAchievement(achievement.getStatus());
        return baseMapper.updateById(achievementFile)==1;
    }

}

